<?php

namespace backend\modules\home\models;

use Yii;

/**
 * This is the model class for table "mh_category".
 *
 * @property int $id
 * @property int $cid
 * @property string $title
 * @property string $title_desc
 * @property string $thumb
 * @property int $sort
 * @property int $create_at
 */
class Category extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mh_category';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['cid', 'create_at','sort'], 'integer'],
            [['title'], 'required'],
            [['title'], 'string', 'max' => 30],
            [['title_desc'], 'string', 'max' => 50],
            [['thumb'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'cid' => 'Cid',
            'title' => '标题',
            'title_desc' => '描述',
            'thumb' => '封面图',
            'sort' => '排序',
            'create_at' => '创建时间',
        ];
    }

    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            if($this->isNewRecord){
                $this->create_at = time();
                if(empty($this->sort)){
                    $this->sort = 0;
                }
            }
            return true;
        }
        return false;
    }
}
